package com.hawthorn.admin.service.sysdealer.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hawthorn.admin.model.po.SysDealerPO;
import com.hawthorn.admin.repository.SysDealerMapper;
import com.hawthorn.admin.service.sysdealer.SysDealerService;
import com.hawthorn.component.constant.FormatConstant;
import com.hawthorn.component.utils.bean.Map2ObjUtil;
import com.hawthorn.platform.exception.BizCode;
import com.hawthorn.platform.service.ExcelApiService;
import com.hawthorn.platform.service.TransResultService;
import com.hawthorn.platform.utils.common.ArrayMyUtil;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.platform.utils.iassert.AssertMyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>todo 渠道商档案表 serviceImp 实现类</p>
 * @author: zhouxin@expservice.com.cn
 * @created: 2022-07-08 3:38 PM
 * @lasted: 2022-07-21 9:40 AM
 * @version v1.0.2
 */
@Service
public class SysDealerServiceImpl implements SysDealerService
{

  @Resource
  private SysDealerMapper sysDealerMapper;

  @Resource
  private TransResultService transResultService;

  @Resource
  private ExcelApiService excelApiService;

  /**
   * <p>todo 查询门店信息</p>
   * @param page
   * @param conditions
   * @return:
   * @author: zhouxin@expservice.com.cn
   * @date: 2022-07-08 3:38 PM
   * @version: 1.0.1
   */
  @Override
  public IPage<Map<String, Object>> find(Page<Map<String, Object>> page, String conditions)
  {
    // 绑定字典
    HashMap<String, String> transHash = new HashMap<>();

    // 设置排序
    OrderItem oi = new OrderItem();

    oi.setColumn("dealerCode");
    oi.setColumn("dealerName");
    oi.setColumn("dealerKind");
    oi.setColumn("orgType");
    oi.setAsc(true);
    transHash.put("createTime", "date|" + FormatConstant.SIMPLE_DATE_TIME_FORMAT);
    transHash.put("lastUpdateTime", "date|" + FormatConstant.SIMPLE_DATE_TIME_FORMAT);
    transHash.put("dealerStatus", "dic|yyzt");
    transHash.put("dealerKind", "dic|qdslb");
    transHash.put("province", "division");
    transHash.put("city", "division");
    transHash.put("town", "division");
    transHash.put("status", "dic|yxzt");
    transHash.put("enterpriseProperty", "dic|qyxz");
    transHash.put("orgType", "dic|zzlx");

    return transResultService.transResult(sysDealerMapper.find(page, conditions), transHash);
  }

  /**
   * <p>todo 保存新增门店信息</p>
   * @param param
   * @return:
   * @author: zhouxin@expservice.com.cn
   * @date: 2022-07-08 3:38 PM
   * @version: 1.0.1
   */
  @Override
  public Map<String, Object> save(Map<String, Object> param)
  {
    SysDealerPO sysDealerPO = Map2ObjUtil.toObject(param, SysDealerPO.class);
    sysDealerMapper.save(sysDealerPO);
    HashMap<String, String> transHash = new HashMap<>();
    return transResultService.transResult(sysDealerPO.toMaps(), transHash);
  }

  /**
   * <p>todo 修改门店信息</p>
   * @param param
   * @return:
   * @author: zhouxin@expservice.com.cn
   * @date: 2022-07-08 3:38 PM
   * @version: 1.0.1
   */
  @Override
  public Map<String, Object> update(Long id, Map<String, Object> param)
  {
    AssertMyUtil.notNull(id, BizCode.METHOD_ARGS_NOTNULL, "id");
    SysDealerPO sysDealerPO = sysDealerMapper.selectById(id);
    Map2ObjUtil.toObject(param, sysDealerPO);
    int result = sysDealerMapper.updateById(sysDealerPO);
    HashMap<String, String> transHash = new HashMap<>();
    return transResultService.transResult(sysDealerPO.toMaps(), transHash);
  }

  /**
   * <p>todo 导出门店信息</p>
   * @param response
   * @param fileName
   * @param params
   * @return:
   * @author: zhouxin@expservice.com.cn
   * @date: 2022-07-08 3:38 PM
   * @version: 1.0.1
   */
  @Override
  public void export(HttpServletResponse response, String fileName, Map<String, Object> params)
  {
    String header = MyUtil.getValue(params.get("header"));
    String param = MyUtil.getValue(params.get("params"));
    String qc = MyUtil.getConditionsWhere(param, null);
    List<List<String>> headList = new ArrayList<>();
    List<String> headFields = excelApiService.toHeader(header, headList);
    String fields = ArrayMyUtil.join(headFields.toArray(), ",");
    List<Map<String, Object>> dataList = sysDealerMapper.export(fields, qc);
    HashMap<String, String> transHash = new HashMap<>();

    excelApiService.export(response, fileName, headFields, headList, dataList, transHash);

  }

  /**
   * <p>todo 根据id修改门店状态</p>
   * @param id
   * @param status
   * @return:
   * @author: zhouxin@expservice.com.cn
   * @date: 2022-07-14 3:52 PM
   * @version: 1.0.1
   */
  @Override
  public Integer changeStatus(Long id, String status)
  {
    AssertMyUtil.notNull(id, BizCode.METHOD_ARGS_NOTNULL, "id");

    JSONObject o = JSONObject.parseObject(status);
    SysDealerPO sysDealerPO = sysDealerMapper.selectById(id);
    sysDealerPO.setStatus(o.getInteger("status"));

    return sysDealerMapper.updateById(sysDealerPO);
  }
}
